"""
Case Type   : DSS元数据修复工具
Case Name   : dsstbox ssrepair参数修改元数据类型为volume_header
Create At   : 2025/04
Owner       : haomeng
Description :
    1、查看当前卷名
    2、使用dsscmd showdisk查看volume_header类型的元数据值
    3、修改部分不支持修改元数据
    4、修改部分支持修改元数据
    5、修改特殊参数software_version
Expect      :
    1、查看成功
    2、查看成功
    3、修改失败
    4、修改成功
    5、修改成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DSSrepairtools(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()

        self.vol_path = os.path.join(macro.DSS_INSTANCE_PATH, 'cfg/dss_vg_conf.ini')
        self.metadata = ['checksum', 'software_version','vol_type.type', 'vol_type.id',
                         'vol_type.entry_volume_name', 'vg_name', 'valid_flag',
                         'create_time.tv_sec', 'create_time.tv_usec', 'bak_level',
                         'ft_node_ratio', 'bak_ft_offset']
        self.volume_header = []

    def test_dsstbox(self):
        text = '-----step1:查看当前卷名;  expect:查看成功-----'
        self.log.info(text)

        res = self.user_node.sh(f"cat {self.vol_path} | awk 'NR==1'").result()
        self.log.info(res)
        vol_name = res.split(':')[0]
        vol_value = res.split(':')[1]
        self.log.info(f'vol_name的值是：{vol_name}, vol_value的值是：{vol_value}')
        self.assertNotEqual('', vol_name, '查看当前卷名失败')

        text = '-----step2:使用dsscmd showdisk查看vg_header类型的元数据值;  expect:修改成功-----'
        self.log.info(text)

        cmd = f"source {macro.DB_ENV_PATH};dsscmd showdisk -g {vol_name} -s vg_header"
        self.log.info(cmd)
        execute_cmd = self.user_node.sh(cmd).result()
        self.log.info(execute_cmd)
        self.volume_header.append(execute_cmd.splitlines())
        self.log.info(self.volume_header)

        sum_volume_header = sum(len(item) for sublist in self.volume_header for item in sublist)
        self.log.info(sum_volume_header)
        self.assertIn('Succeed to print dss metadata', execute_cmd, '查看元数据结果失败')

        text = '-----step3:修改部分不支持修改元数据;  expect:修改失败-----'
        self.log.info(text)

        self.metadata_value = [1503656919, 2, 305419897, 0, f'{vol_value}', f'{vol_name}',
                               1597463007, 429496729, 1000, 1, 100, 1]

        cmd1 = f"dsstbox ssrepair -v {vol_value} -t volume_header -k '{self.metadata[0]}={self.metadata_value[0]}'"
        self.log.info(cmd1)
        execute_cmd1 = f'''source {macro.DB_ENV_PATH};yes|{cmd1}'''
        self.log.info(execute_cmd1)
        res1 = self.user_node.sh(execute_cmd1).result()
        self.log.info(res1)
        self.assertIn('Failed to execute repair meta info', res1, '修改元数据成功')

        text = '-----step4:修改部分支持修改元数据;  expect:修改成功-----'
        self.log.info(text)

        for param in self.metadata[2:]:
            cmd2 = f"dsstbox ssrepair -v {vol_value} -t volume_header -k '{param}={self.metadata_value[self.metadata.index(param)]}'"
            self.log.info(cmd2)
            execute_cmd2 = f'source {macro.DB_ENV_PATH};yes|{cmd2}'
            self.log.info(execute_cmd2)
            res2 = self.user_node.sh(execute_cmd2).result()
            self.log.info(res2)
            self.assertIn('Succeed to execute repair meta info', res2, '修改元数据失败')

        text = '-----step5:修改特殊参数software_version;  expect:修改成功-----'
        self.log.info(text)
        cmd3 = f"dsstbox ssrepair -v {vol_value} -t {self.metadata[1]} -k '{self.metadata[1]}={self.metadata_value[1]}'"
        self.log.info(cmd3)
        execute_cmd3 = f'''source {macro.DB_ENV_PATH};yes|{cmd3}'''
        self.log.info(execute_cmd3)
        res3 = self.user_node.sh(execute_cmd3).result()
        self.log.info(res3)
        self.assertIn('Succeed to execute repair meta info', res3, '修改元数据失败')

    def tearDown(self):
        self.log.info('-----No need to clean-----')
        self.log.info(f'----{os.path.basename(__file__)} end----')
